const {storeBitPacket, stringToBytes, parseBitPacket} = require("./utils")
class Packet {
    constructor(p) {
        this.packet = p || [];
        this.offset = 0;
    }

    extract(bit) {
        let value = parseBitPacket(this.packet, this.offset, bit);
        this.offset += bit;
        return value;
    }

    extractChunk(bit) {
        let chunk = this.packet.slice(Math.floor(this.offset / 8), Math.floor((this.offset + bit)/8));
        this.offset += bit;
        return chunk;
    }

    fill(value, bit) {
        storeBitPacket(this.packet, value, this.offset, bit);
        this.offset += bit;
        return this;
    }

    fillChunk(chunk, bit) {
        this.packet.push(...chunk);
        this.offset += bit;
        return this;
    }

    getPacket() {
        return this.packet;
    }
}

module.exports = Packet;